# -*- coding: utf-8 -*-
"""
@Time ： 2023/10/9 12:38
@Auth ： y.h
@File ：pandas_utils.py
@IDE ：PyCharm
@Motto：The sooner you start to code, the longer the program will take. —— Roy Carlson
"""

import pandas as pd
from pythonProjectTemplate.Entity.SystemEntity.get_sheet_data_value_entity import 数据裁切配置文件


def get_sheet_data(data_dict: dict, properties: 数据裁切配置文件) -> dict:
    df = pd.DataFrame()
    if type(properties) != None:
        if len(properties.get_col_index_list()) < 1 and len(
                properties.get_line_index_list()) < 1 and properties.get_col_end_index() - properties.get_col_begin_index() < 0 and properties.get_row_end_index() - properties.get_row_begin_index() < 0:
            print("列或行参数未定义!")
            exit(True)
        else:
            if properties.get_file_name() in data_dict.keys():
                if properties.get_sheet_name() in data_dict.get(properties.get_file_name()):
                    if len(properties.get_col_index_list()) > 0 and len(properties.get_line_index_list()) > 0:
                        sheet_name = data_dict.get(properties.get_file_name()).get(properties.get_sheet_name())
                        result = sheet_name.iloc[properties.get_line_index_list(), properties.get_col_index_list()]
                        return result

                    # 当列为空,获取整行数据
                    elif len(properties.get_col_index_list()) < 1 and properties.get_row_begin_index() < 1 and properties.get_col_begin_index() < 1:
                        sheet_name = data_dict.get(properties.get_file_name()).get(properties.get_sheet_name())
                        result = sheet_name.iloc[properties.get_line_index_list()]
                        return result

                    # 当行为空,获取整列数据
                    elif len(properties.get_line_index_list()) < 1 and properties.get_row_begin_index() < 1 and properties.get_col_begin_index() < 1:
                        sheet_name = data_dict.get(properties.get_file_name()).get(properties.get_sheet_name())
                        result = sheet_name.iloc[:, properties.get_col_index_list()]
                        return result

                    # 当配置了行列起始截止值
                    elif (properties.get_row_end_index() - properties.get_row_begin_index() >= 0
                          and properties.get_col_end_index() - properties.get_col_begin_index() >= 0):
                        print("-----------------test----------------")
                        sheet_name = data_dict.get(properties.get_file_name()).get(properties.get_sheet_name())
                        result = sheet_name.iloc[properties.get_row_begin_index():properties.get_row_end_index(),
                                 properties.get_col_begin_index():properties.get_col_end_index()]
                        return result

                    # 当配置了行起始截止值
                    elif properties.get_row_end_index() - properties.get_row_begin_index() >= 0:
                        sheet_name = data_dict.get(properties.get_file_name()).get(properties.get_sheet_name())
                        result = sheet_name.iloc[properties.get_row_begin_index():properties.get_row_end_index()]
                        return result

                    # 当配置了列起始截止值
                    elif properties.get_row_end_index() - properties.get_row_begin_index() >= 0:
                        sheet_name = data_dict.get(properties.get_file_name()).get(properties.get_sheet_name())
                        result = sheet_name.iloc[:, properties.get_row_begin_index():properties.get_row_end_index()]
                        return result
                    else:
                        print("程序错误")
                        exit(True)

            elif type(data_dict) == type(df):
                if len(properties.get_col_index_list()) > 0 and len(properties.get_line_index_list()) > 0:

                    result = data_dict.iloc[properties.get_line_index_list(), properties.get_col_index_list()]
                    return result

                # 当列为空,获取整行数据
                elif len(properties.get_col_index_list()) < 1 and properties.get_row_begin_index() < 1 and properties.get_col_begin_index() < 1:

                    result = data_dict.iloc[properties.get_line_index_list()]
                    return result

                # 当行为空,获取整列数据
                elif len(properties.get_line_index_list()) < 1 and properties.get_row_begin_index() < 1 and properties.get_col_begin_index() < 1:

                    result = data_dict.iloc[:, properties.get_col_index_list()]
                    return result

                    # 当配置了行列起始截止值
                elif (properties.get_row_end_index() - properties.get_row_begin_index() >= 0
                      and properties.get_col_end_index() - properties.get_col_begin_index() >= 0):
                    result = data_dict.iloc[properties.get_row_begin_index():properties.get_row_end_index(),
                             properties.get_col_begin_index():properties.get_col_end_index()]
                    return result

                # 当配置了行起始截止值
                elif properties.get_row_end_index() - properties.get_row_begin_index() >= 0:
                    result = data_dict.iloc[properties.get_row_begin_index():properties.get_row_end_index()]
                    return result

                # 当配置了列起始截止值
                elif properties.get_row_end_index() - properties.get_row_begin_index() >= 0:
                    result = data_dict.iloc[:, properties.get_row_begin_index():properties.get_row_end_index()]
                    return result
                else:
                    print("程序错误")
                    exit(True)

    else:
        print("配置文件为空!")
        exit(True)


# 获取列数
def get_col_count(df):
    return df.shape[1]


# 获取行数
def get_row_count(df):
    return df.shape[0]


# 条件查询
def condition_query(df, query_condition: str):
    return df[eval(query_condition)]


# 模糊查询
def fuzzy_query(df, col_name: str, like_value: str):
    return df[col_name].map(lambda x: x.startswith(like_value))
